﻿//Durch den Typ dynamics, ist das _objJsonSeed auch volgendermaßen nutzbar:
//Object obj = _objJsonSeed["wilderness"]["city" + CityID];
//Da aber auf Ausfallsicherheit Wert liegt, wird weiterhin vernünftig gecastet. :)

using System;
using System.Collections;
using System.Collections.ObjectModel;
using AutoKoC.Models;
using AutoKoC.ViewModels.ItemClasses;
using CSharp.Libs.WPF;
using CSharp.Libs.Koc;
using System.Collections.Generic;


namespace AutoKoC.ViewModels
{
    class BaseSeedViewModel : ViewModelBase
    {
        #region declarations
        private AppBase _AB = null;

        private dynamic _objJsonSeed = null;

        private Hashtable _htUpdateSeed = null;

        private ObservableCollection<SeedCityItem> _ocCityItems = new ObservableCollection<SeedCityItem>();
        private Dictionary<String, Dictionary<String, MarchItem>> _dictMarches = new Dictionary<string, Dictionary<string, MarchItem>>();

        #region events
        public delegate void SeedUpdatedEventHandler(object sender);
        public event SeedUpdatedEventHandler SeedUpdated;
        private void ThrowSeedUpdatedEvent()
        {
            if (SeedUpdated != null)
            {
                SeedUpdated(this);
            }
        }
        #endregion
        #endregion



        #region constructor / terminator
        public BaseSeedViewModel(
            AppBase AB)
        {
            _AB = AB;

            _AB.TM.UpdateSeedReceived += new TrafficMonitor.UpdateSeedReceivedEventHandler(TM_UpdateSeedReceived);
        }

        ~BaseSeedViewModel()
        {
            _objJsonSeed = null;
            _ocCityItems = null;

            if (_AB != null)
            {
                if (_AB.TM != null)
                {
                    _AB.TM.UpdateSeedReceived -= TM_UpdateSeedReceived;
                }

                _AB = null;
            }
        }
        #endregion



        #region event handling
        void TM_UpdateSeedReceived(object sender, UpdateSeedReceivedEventArgs ea)
        {
            try
            {
                _htUpdateSeed = ea.UpdateSeed;
                _UpdateSeedReceived();
            }
            catch (Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion



        #region private functions
        /// <summary>_SeedRefreshed
        /// 
        /// </summary>
        private void _SeedRefreshed()
        {
            try
            {
                Hashtable htSeed = _objJsonSeed as Hashtable;
                if(htSeed != null)
                {
                    // first off get cities.
                    ArrayList aryCities = htSeed[constants.KEY_SEED_CITIES] as ArrayList;
                    Hashtable htResources = htSeed[constants.KEY_SEED_RESOURCES] as Hashtable;
                    if(aryCities != null &&
                       htResources != null)
                    {
                        _RefreshCities(aryCities, htResources);
                    }
                }
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_UpdateSeedReceived</summary>
        /// <history>10.05.2011</history>
        private void _UpdateSeedReceived()
        {
            try
            {
                if (_htUpdateSeed != null)
                {
                    OnPropertyChanged("UpdateSeed_reqmicrotime");
                    OnPropertyChanged("UpdateSeed_resmicrotime");
                    
                    //get updateSeed
                    Hashtable htUpdateSeed = _htUpdateSeed[constants.KEY_UPDATE_SEED] as Hashtable;
                    if (htUpdateSeed != null)
                    {
                        //get updateSeedCities
                        Hashtable htUpdateSeedCities = htUpdateSeed[constants.KEY_CITY] as Hashtable;
                        if (htUpdateSeedCities != null)
                        {
                            //Update CityData
                            foreach (SeedCityItem SCI in _ocCityItems)
                            {
                                //Get updateSeedCity
                                Hashtable htUpdateSeedCity = htUpdateSeedCities[SCI.CityID.ToString()] as Hashtable;
                                if (htUpdateSeedCity != null)
                                {
                                    //Get updateSeedCityProduction
                                    Hashtable htUpdateSeedCityProduction = htUpdateSeedCity[constants.KEY_PRODUCTION] as Hashtable;
                                    if (htUpdateSeedCityProduction != null)
                                    {
                                        SCI.Resources.Food.Value = Int64.Parse(htUpdateSeedCityProduction[constants.KEY_RESOURCE_1_X_3600].ToString()) / 3600;
                                        SCI.Resources.Wood.Value = Int64.Parse(htUpdateSeedCityProduction[constants.KEY_RESOURCE_2_X_3600].ToString()) / 3600;
                                        SCI.Resources.Stone.Value = Int64.Parse(htUpdateSeedCityProduction[constants.KEY_RESOURCE_3_X_3600].ToString()) / 3600;
                                        SCI.Resources.Ore.Value = Int64.Parse(htUpdateSeedCityProduction[constants.KEY_RESOURCE_4_X_3600].ToString()) / 3600;
                                    }

                                    //TODO: Get updateSeedCityConstructuion
                                    //TODO: Get updateSeedCityResearch
                                }
                            }
                        }
                    }

                    //Check for updateMarch
                    Hashtable htUpdateMarch = _htUpdateSeed[constants.KEY_UPDATE_MARCH] as Hashtable;
                    if (htUpdateMarch != null)
                    {
                        foreach (DictionaryEntry deMarchCity in htUpdateMarch)
                        { 
                            Int32 intUpdateMarchCityID = 0;
                            Int32.TryParse(deMarchCity.Key.ToString().Replace("c", "").ToString(), out intUpdateMarchCityID);

                            Hashtable htCityMarches = deMarchCity.Value as Hashtable;
                            if (htCityMarches != null)
                            {
                                foreach (DictionaryEntry deMarches in htCityMarches)
                                {
                                    Int32 intMarchID = 0;
                                    Int32.TryParse(deMarches.Key.ToString().Replace("m", "").ToString(), out intMarchID);

                                    ArrayList aryMarch = deMarches.Value as ArrayList;
                                    if (aryMarch != null)
                                    {
                                        //Go through each march
                                        foreach (Hashtable htMarch in aryMarch)
                                        {
                                            _UpdateMarchItem(intMarchID, htMarch);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //Throw event after handling
                    ThrowSeedUpdatedEvent();
                }
            }
            catch (Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_RefreshCities
        /// 
        /// </summary>
        /// <param name="Cities"></param>
        private void _RefreshCities(
            ArrayList Cities,
            Hashtable Resources)
        {
            try
            {
                if(Cities.Count > 0)
                {
                    _ocCityItems.Clear();

                    foreach(ArrayList aryCity in Cities)
                    {
                        SeedCityItem SCI = new SeedCityItem();
                        SCI.CityID = Int32.Parse(aryCity[0].ToString());
                        SCI.CityName = aryCity[1].ToString();
                        SCI.xCoord = Int32.Parse(aryCity[2].ToString());
                        SCI.yCoord = Int32.Parse(aryCity[3].ToString());
                        SCI.ProvinceID = aryCity[4].ToString();
                        SCI.MapTileID = aryCity[5].ToString();
                        SCI.Resources = _GetResources(SCI, Resources);
                        SCI.RallyPointLevel = _GetCityRallyPointLevel(SCI.CityID);

                        _GetResourceProduction(SCI);

                        _GetKnights(SCI);

                        _ocCityItems.Add(SCI);
                    }
                }
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_GetResources
        /// 
        /// </summary>
        /// <param name="Resources"></param>
        /// <history>24.04.2011</history>
        private SeedCityResourcesItem _GetResources(
            SeedCityItem SCI,
            Hashtable Resources)
        {
            try
            {
                if(SCI.CityID > 0 &&
                   Resources.Count > 0)
                {
                    Hashtable htCityResources = Resources[String.Concat("city", SCI.CityID.ToString())] as Hashtable;
                    if(htCityResources != null)
                    {
                        SeedCityResourcesItem SRI = new SeedCityResourcesItem();

                        SRI.Tick = Int32.Parse(htCityResources["tick"].ToString());
                        
                        ArrayList res=null;
                        Int64 retVal = 0;

                        // get food
                        res = htCityResources["rec1"] as ArrayList;
                        if(Int64.TryParse(res[0].ToString(), out retVal))
                        {
                            SRI.Food.Value = retVal / 3600;
                        }
                        SRI.Food.Unknown1 = res[1].ToString();
                        SRI.Food.Unknown2 = res[2].ToString();
                        if(Int64.TryParse(res[3].ToString(), out retVal))
                        {
                            SRI.Food.Usage = (Int32)retVal;
                        }

                        // get wood
                        res = htCityResources["rec2"] as ArrayList;
                        if(Int64.TryParse(res[0].ToString(), out retVal))
                        {
                            SRI.Wood.Value = retVal / 3600;
                        }
                        SRI.Wood.Unknown1 = res[1].ToString();
                        SRI.Wood.Unknown2 = res[2].ToString();
                        if(Int64.TryParse(res[3].ToString(), out retVal))
                        {
                            SRI.Wood.Usage = (Int32)retVal;
                        }

                        // get stone
                        res = htCityResources["rec3"] as ArrayList;
                        if(Int64.TryParse(res[0].ToString(), out retVal))
                        {
                            SRI.Stone.Value = retVal / 3600;
                        }
                        SRI.Stone.Unknown1 = res[1].ToString();
                        SRI.Stone.Unknown2 = res[2].ToString();
                        if(Int64.TryParse(res[3].ToString(), out retVal))
                        {
                            SRI.Stone.Usage = (Int32)retVal;
                        }

                        // get ore
                        res = htCityResources["rec4"] as ArrayList;
                        if(Int64.TryParse(res[0].ToString(), out retVal))
                        {
                            SRI.Ore.Value = retVal / 3600;
                        }
                        SRI.Ore.Unknown1 = res[1].ToString();
                        SRI.Ore.Unknown2 = res[2].ToString();
                        if(Int64.TryParse(res[3].ToString(), out retVal))
                        {
                            SRI.Ore.Usage = (Int32)retVal;
                        }

                        return SRI;
                    }
                }
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }

            return new SeedCityResourcesItem();
        }

        /// <summary>_GetResourceProduction
        /// 
        /// </summary>
        /// <history>03.05.2011</history>
        private void _GetResourceProduction(
            SeedCityItem SCI)
        {
            try
            {
                //put together relevant variables
                Double[] _arWildLevels = new Double[5] { 0, 0, 0, 0, 0 };
                Double _dblKnight = 0;
                Double _dblWorkerFactor = 1;
                Double[] _arItems = new Double[5] { 0, 0, 0, 0, 0 };

                //prepare needed variables
                Hashtable htSeed = _objJsonSeed as Hashtable;
                Int32 _intUnixNow = _AB.conv_Date2Timestamp(DateTime.Now);

                #region get wild levels
                //var wilds = [0, 0, 0, 0, 0];
                //var w = Seed.wilderness["city" + cityId];
  
                //for (var k in w)
                //{
                //var type = parseInt(w[k].tileType);
	
                //if (type==10 || type==11)
                //    wilds[1] += parseInt(w[k].tileLevel);
                //else 
                //    wilds[type/10] += parseInt(w[k].tileLevel);
                //}  

                //get wilderness Hashtable
                Hashtable htWilds = htSeed["wilderness"] as Hashtable;
                if (htWilds != null)
                {
                    //get wilds of city Hashtable
                    Hashtable htCityWilds = htWilds["city" + SCI.CityID] as Hashtable;
                    if (htCityWilds != null)
                    {
                        foreach (DictionaryEntry DE in htCityWilds)
                        {
                            //get each city wild Hashtable
                            Hashtable htCityWild = DE.Value as Hashtable;
                            if (htCityWild != null)
                            {
                                //get type and level of each wild
                                Int32 intTileType = Int32.Parse(htCityWild["tileType"].ToString());
                                Int32 intTileLevel = Int32.Parse(htCityWild["tileLevel"].ToString());

                                if (intTileType == 10 || intTileType == 11)
                                {
                                    _arWildLevels[1] += intTileLevel;
                                }
                                else
                                {
                                    _arWildLevels[intTileType / 10] += intTileLevel;
                                }
                            }
                        }

                    }
                }
                #endregion

                #region get steward knight resourcefulness
                //knight = 0;
                //var s = Seed.knights["city" + cityId];

                //if (s)
                //{
                //    s = s["knt" + Seed.leaders["city" + cityId].resourcefulnessKnightId];
                //    if (s)
                //    {
                //        var knight = parseInt(s.resourcefulness);
                //        if (s.resourcefulnessBoostExpireUnixtime > now)
                //            knight *= 1.25;
                //    }
                //}
                
                //get knights and leaders Hashtable
                Hashtable htKnights = htSeed["knights"] as Hashtable;
                Hashtable htLeaders = htSeed["leaders"] as Hashtable;
                if (htKnights != null && htLeaders != null)
                {
                    //get knights and leaders of city
                    Hashtable htCityKnights = htKnights["city" + SCI.CityID] as Hashtable;
                    Hashtable htCityLeaders = htLeaders["city" + SCI.CityID] as Hashtable;
                    if (htCityKnights != null && htCityLeaders != null)
                    {
                        //get ID of steward knight
                        Int32 intStewardKnightId = Int32.Parse(htCityLeaders["resourcefulnessKnightId"].ToString());
                        if (intStewardKnightId > 0)
                        {
                            //get steward knight
                            Hashtable htStewardKnight = htCityKnights["knt" + intStewardKnightId] as Hashtable;
                            if (htStewardKnight != null)
                            {
                                //get resourcefulness
                                _dblKnight = Int32.Parse(htStewardKnight["resourcefulness"].ToString());
                                
                                //get boost if exists (check for leading zero which would be year 0)
                                String _strResourcefulnessBoostExpiration = htStewardKnight["resourcefulnessBoostExpiration"].ToString();
                                if (!_strResourcefulnessBoostExpiration.StartsWith("0"))
                                {
                                    Int32 _intBoostExpirationTime = _AB.conv_Date2Timestamp(DateTime.Parse(_strResourcefulnessBoostExpiration));

                                    if (_intBoostExpirationTime > _intUnixNow)
                                    {
                                        _dblKnight *= 1.25;
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                #region get worker factor
                //var workerFactor = 1;
                //var c = parseInt(Seed.citystats["city" + cityId]["pop"][0]);  // Current  population
                //var w = parseInt(Seed.citystats["city" + cityId]["pop"][3]);  // Labor force
                //if (w > c)
                //    workerFactor = c / w;

                //get citystats Hashtable
                Hashtable htCityStats = htSeed["citystats"] as Hashtable;
                if (htCityStats != null)
                {
                    //get citystats of city
                    Hashtable htCityCityStats = htCityStats["city" + SCI.CityID] as Hashtable;
                    if (htCityCityStats != null)
                    {
                        //get population info
                        ArrayList htPop = htCityCityStats["pop"] as ArrayList;
                        if (htPop != null)
                        {
                            Double _dblCurrentPopulation = Double.Parse(htPop[0].ToString()); // population overall
                            Double _dblLaborForce = Double.Parse(htPop[3].ToString());        // working population

                            if (_dblLaborForce > _dblCurrentPopulation)
                            {
                                _dblWorkerFactor = _dblCurrentPopulation / _dblLaborForce;
                            }
                        }
                    }
                }
                #endregion

                #region get items factor
                #endregion

                #region calculate resources
                //for (var i = 1; i < 5; i++)
                //{
                //    var usage = Seed.resources["city" + cityId]["rec" + i];
                //    var items = 0;

                //    if (parseInt(Seed.playerEffects["r" + i + "BstExp"]) > now)
                //    {
                //        items = 0.25;
                //    }

                //    var tech = Seed.tech["tch" + i];

                //    ret[i] = parseInt((usage[2] * (1 + tech / 10 + knight / 100 + items + 0.05 * wilds[i]) * workerFactor + 100));
                //}

                // 1: Food
                // 2: Wood
                // 3: Stone
                // 4: Ore

                Hashtable htPlayerEffects = htSeed["playerEffects"] as Hashtable;
                Hashtable htTech = htSeed["tech"] as Hashtable;
                if (htPlayerEffects != null && htTech != null)
                {
                    for (Int32 IDX = 1; IDX < 5; IDX++)
                    {
                        Object objBoostExpireDate = htPlayerEffects["r" + IDX + "BstExp"] as Object;
                        if (objBoostExpireDate != null)
                        {
                            String _strBoostExpireDate = htPlayerEffects["r" + IDX + "BstExp"] as String;
                            if (_strBoostExpireDate != null &&
                                _strBoostExpireDate != "" && 
                                !_strBoostExpireDate.StartsWith("0"))
                            {
                                _arItems[IDX] = 0.25;
                            }
                        }

                        Double dblTech = Double.Parse(htTech["tch" + IDX].ToString());

                        switch (IDX)
                        {
                            case 1:
                                SCI.Resources.Food.Production = (Int32)(Int32.Parse(SCI.Resources.Food.Unknown2) * (1 + dblTech / 10 + _dblKnight / 100 + _arItems[IDX] + 0.05 * _arWildLevels[IDX]) * _dblWorkerFactor + 100);
                                break;
                            case 2:
                                SCI.Resources.Wood.Production = (Int32)(Int32.Parse(SCI.Resources.Wood.Unknown2) * (1 + dblTech / 10 + _dblKnight / 100 + _arItems[IDX] + 0.05 * _arWildLevels[IDX]) * _dblWorkerFactor + 100);
                                break;
                            case 3:
                                SCI.Resources.Stone.Production = (Int32)(Int32.Parse(SCI.Resources.Stone.Unknown2) * (1 + dblTech / 10 + _dblKnight / 100 + _arItems[IDX] + 0.05 * _arWildLevels[IDX]) * _dblWorkerFactor + 100);
                                break;
                            case 4:
                                SCI.Resources.Ore.Production = (Int32)(Int32.Parse(SCI.Resources.Ore.Unknown2) * (1 + dblTech / 10 + _dblKnight / 100 + _arItems[IDX] + 0.05 * _arWildLevels[IDX]) * _dblWorkerFactor + 100);
                                break;
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_GetKnights</summary>
        /// <param name="SCI"></param>
        /// <history>11.05.2011</history>
        private void _GetKnights(
            SeedCityItem SCI)
        {
            try
            {
                Hashtable htSeed = _objJsonSeed as Hashtable;
                if (htSeed != null)
                {
                    Hashtable htKnights = htSeed[constants.KEY_SEED_KNIGHTS] as Hashtable;
                    if (htKnights != null)
                    {
                        Hashtable htCityKnights = htKnights["city" + SCI.CityID.ToString()] as Hashtable;
                        if (htCityKnights != null)
                        {
                            //Get each Knight
                            foreach (DictionaryEntry DE in htCityKnights)
                            {
                                Hashtable htCityKnight = DE.Value as Hashtable;
                                if (htCityKnight != null)
                                {
                                    SeedCityKnightItem SCKI = new SeedCityKnightItem();
                                    SCKI.BaseCombat = htCityKnight[constants.KEY_SEED_BASE_COMBAT].ToString();
                                    SCKI.BaseIntelligence = htCityKnight[constants.KEY_SEED_BASE_INTELLIGENCE].ToString();
                                    SCKI.BasePolitics = htCityKnight[constants.KEY_SEED_BASE_POLITICS].ToString();
                                    SCKI.BaseResourcefulness = htCityKnight[constants.KEY_SEED_BASE_RESOURCEFULNESS].ToString();
                                    SCKI.CityId = htCityKnight[constants.KEY_SEED_CITY_ID].ToString();
                                    SCKI.Combat = htCityKnight[constants.KEY_SEED_COMBAT].ToString();
                                    SCKI.CombatBoostExpiration = htCityKnight[constants.KEY_SEED_COMBAT_BOOST_EXPIRATION].ToString();
                                    SCKI.CombatBoostExpireUnixtime = htCityKnight[constants.KEY_SEED_COMBAT_BOOST_EXPIRE_UNIX_TIME].ToString();
                                    SCKI.Experience = htCityKnight[constants.KEY_SEED_EXPERIENCE].ToString();
                                    SCKI.Intelligence = htCityKnight[constants.KEY_SEED_INTELLIGENCE].ToString();
                                    SCKI.IntelligenceBoostExpiration = htCityKnight[constants.KEY_SEED_INTELLIGENCE_BOOST_EXPIRATION].ToString();
                                    SCKI.IntelligenceBoostExpireUnixtime = htCityKnight[constants.KEY_SEED_INTELLIGENCE_BOOST_EXPIRE_UNIX_TIME].ToString();
                                    SCKI.KnightFbuid = htCityKnight[constants.KEY_SEED_KNIGHT_FBUID].ToString();
                                    SCKI.KnightId = htCityKnight[constants.KEY_SEED_KNIGHT_ID].ToString();
                                    SCKI.KnightLevel = htCityKnight[constants.KEY_SEED_KNIGHT_LEVEL].ToString();
                                    SCKI.KnightLordUserid = htCityKnight[constants.KEY_SEED_KNIGHT_LORD_USER_ID].ToString();
                                    SCKI.KnightName = htCityKnight[constants.KEY_SEED_KNIGHT_NAME].ToString();
                                    SCKI.KnightStatus = htCityKnight[constants.KEY_SEED_KNIGHT_STATUS].ToString();
                                    SCKI.Loyalty = htCityKnight[constants.KEY_SEED_LOYALTY].ToString();
                                    SCKI.LoyaltyTimestamp = htCityKnight[constants.KEY_SEED_LOYALTY_TIMESTAMPT].ToString();
                                    SCKI.LoyaltyUnixtime = htCityKnight[constants.KEY_SEED_LOYALTY_UNIX_TIME].ToString();
                                    if(htCityKnight[constants.KEY_SEED_PIC_SQUARE] != null)
                                    {
                                        SCKI.Pic_square = htCityKnight[constants.KEY_SEED_PIC_SQUARE].ToString();
                                    }
                                    SCKI.Politics = htCityKnight[constants.KEY_SEED_POLITICS].ToString();
                                    SCKI.PoliticsBoostExpiration = htCityKnight[constants.KEY_SEED_POLITICS_BOOST_EXPIRATION].ToString();
                                    SCKI.PoliticsBoostExpireUnixtime = htCityKnight[constants.KEY_SEED_POLITICS_BOOST_EXPIRE_UNIX_TIME].ToString();
                                    SCKI.Resourcefulness = htCityKnight[constants.KEY_SEED_RESOURCEFULNESS].ToString();
                                    SCKI.ResourcefulnessBoostExpiration = htCityKnight[constants.KEY_SEED_RESOURCEFULNESS_BOOST_EXPIRATION].ToString();
                                    SCKI.ResourcefulnessBoostExpireUnixtime = htCityKnight[constants.KEY_SEED_RESOURCEFULNESS_BOOST_EXPIRE_UNIX_TIME].ToString();
                                    SCKI.SkillPointsApplied = htCityKnight[constants.KEY_SEED_SKILL_POINTS_APPLIED].ToString();
                                    
                                    SCI.Knights.Add(DE.Key.ToString(), SCKI);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }

        /// <summary>_GetCityRallyPointLevel</summary>
        /// <param name="CityID"></param>
        /// <returns></returns>
        /// <history>05.06.2011</history>
        private Int32 _GetCityRallyPointLevel(
            Int32 CityID)
        {
            try
            {
                Hashtable htSeed = _objJsonSeed as Hashtable;
                Hashtable htBuildings = htSeed[constants.KEY_SEED_BUILDINGS] as Hashtable;

                if(htBuildings != null)
                {
                    Hashtable htCity = htBuildings["city" + CityID.ToString()] as Hashtable;

                    if(htCity != null)
                    {
                        ArrayList aryPos = null;

                        foreach(DictionaryEntry DE in htCity)
                        {
                            aryPos = DE.Value as ArrayList;

                            Int32 intBuildingType = _AB.GetInt32(aryPos[0]);

                            if(intBuildingType == enums.BuildingTypes.RallyPoint.GetHashCode())
                            {
                                return _AB.GetInt32(aryPos[1]);
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }

            return -1;
        }

        /// <summary>_UpdateMarchItem</summary>
        /// <param name="htMarchItem"></param>
        /// <history>14.06.2011</history>
        private void _UpdateMarchItem(
            Int32 MarchID,
            Hashtable htMarchItem)
        {
            try
            {
                MarchItem MI = null;

                if(_AB.Marches.ContainsKey(MarchID))
                {
                    MI = _AB.Marches[MarchID];

                    // TODO: Hier das Marchitem updaten. Überlebt Truppen Zahlen gehen nach unitReturn. Vorher das Item noch um Loot erweitern und Feind Truppen gekämpft/überlebt.
                }

                if(_AB.DBOps.MarchesItemExists(MarchID))
                {
                    //Get March off DB and Update it.
                    MI = _AB.DBOps.MarchesGetItem(MarchID);

                    if(MI != null)
                    {
                        //Exists, also update dies Ding.

                        //MI.CityID

                        //_AB.DBOps.MarchesUpdateItem(MI);
                    }
                }
                else
                {
                    //Exists nicht, also create das Ding.
                }
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion



        #region interface
        #region properties
        public object JsonSeed
        {
            get
            {
                return _objJsonSeed;
            }
            set
            {
                if(value != null)
                {
                    _objJsonSeed = value;
                    _SeedRefreshed();
                }
            }
        }

        public ObservableCollection<SeedCityItem> Cities
        {
            get
            {
                return _ocCityItems;
            }
        }

        public String UpdateSeed_reqmicrotime
        {
            get
            {
                if (_htUpdateSeed != null && _htUpdateSeed.ContainsKey(constants.KEY_REQ_MICRO_TIME))
                {
                    return _htUpdateSeed[constants.KEY_REQ_MICRO_TIME].ToString();
                }
                else
                {
                    return "reqmicrotime does not exist !?!!?!?!";
                }
            }
        }

        public String UpdateSeed_resmicrotime
        {
            get
            {
                if (_htUpdateSeed != null && _htUpdateSeed.ContainsKey(constants.KEY_RES_MICRO_TIME))
                {
                    return _htUpdateSeed[constants.KEY_RES_MICRO_TIME].ToString();
                }
                else
                {
                    return "resmicrotime does not exist !?!!?!?!";
                }
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
